package ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import computation.Clustering;

import primatives.PointND;
import util.epsNet_ReductionEditor.NumericKeyAdapter;

public class CylinderClusteringFrame extends JDialog
{
	// TODO: all the text fields create a new numeric key adapter, perhaps create just one and get all the text fields to use it?
	public class NumericKeyAdapter extends KeyAdapter
	{
		public void keyTyped(KeyEvent e)
		{
			// eat any characters that are not digits (and some special characters like backspace)
			char c = e.getKeyChar();
            if (!((c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE) ||  (c== KeyEvent.VK_ENTER)  || (c == KeyEvent.VK_TAB) ||
            		(c==KeyEvent.VK_DECIMAL) || (c==KeyEvent.VK_PERIOD) || (c==KeyEvent.VK_MINUS) || (Character.isDigit(c)))) e.consume();
		}
	}
	
	public JButton btnRunClustering;
	
	// clustering parameters
	public JTextField txtEps;
	public JTextField txtD;
	
	private Window_ViewerApp parent;
	public CylinderClusteringFrame(Window_ViewerApp window)
	{
		super(window);
		setTitle("Cylinder Clustering Parameters");
		parent = window;
		setupUI();
		
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	private void setupUI()
	{		
		txtEps = new JTextField("5", 5); // perhaps this could be a constant times the minimal distance since we are already sorting the points... it would be trival to find the minimal distance in O(n) time
		txtD = new JTextField("0.5", 5); // same with this parameter... the maximum diameter can be found in the same run by computing a bounding box.... the max of the X and Y directions
		
		txtEps.addKeyListener(new NumericKeyAdapter());
		txtD.addKeyListener(new NumericKeyAdapter());
		
		btnRunClustering = new JButton("Run Clustering");
		btnRunClustering.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				double eps = Double.parseDouble(txtEps.getText());
				double d = Double.parseDouble(txtD.getText());
				parent.canvas.viewer.find_clusters(eps, d);
			}
		});
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.WEST;
		
		c.gridx = 0;
		c.gridy = 0;
		add(new JLabel("Eps: "));
		
		c.gridx = 1;
		c.gridy = 0;
		add(txtEps);
		
		c.gridx = 0;
		c.gridy = 1;		
		add(new JLabel("Radius: "));
		
		c.gridx = 1;
		c.gridy = 1;		
		add(txtD);
		
		c.gridx = 0;
		c.gridy = 2;
		c.weightx = 2.0;
		add(btnRunClustering);
		
		pack();
	}
}
